package jin.chen.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jin.chen.pojo.Carousel;
import jin.chen.pojo.Category;
import jin.chen.pojo.UserAddress;
import jin.chen.pojo.bo.AddressBo;
import jin.chen.pojo.vo.CategoryVO;
import jin.chen.pojo.vo.SixNewItemVO;
import jin.chen.service.AddressService;
import jin.chen.service.CarouselService;
import jin.chen.service.CategoryService;
import jin.chen.utils.MobileEmailUtils;
import jin.chen.utils.Result;
import jin.chen.utils.enums.CarouselShowEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货地址相关的controller
 */
@Api(value = "收货地址", tags = "收货地址controller")
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 根据userId查询所有收货地址
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取所有收货地址接口", notes = "获取所有收货地址接口", httpMethod = "POST")
    @PostMapping("/list")
    public Result getAllAddress(@ApiParam(name = "userId", value = "用户id", required = true)
                                @RequestParam String userId){
        if (userId == null){
            return Result.errorMsg("");
        }
        List<UserAddress> list =  addressService.queryAllAddress(userId);
        return Result.OK(list);
    }

    /**
     * 新增收货地址
     * @param addressBo
     * @return
     */
    @ApiOperation(value = "新增收货地址接口", notes = "新增收货地址接口", httpMethod = "POST")
    @PostMapping("/add")
    public Result addAddress(@ApiParam(name = "addressBo", value = "新增收货地址前端业务Bo", required = true)
                                @RequestBody AddressBo addressBo){
        //先校验前端传来的数据是否正确
        Result result = checkAddress(addressBo);
        if(result.getStatus() != 200){
            return result;
        }
        addressService.addAddress(addressBo);
        return Result.OK();
    }

    /**
     * 修改收货地址
     * @param addressBo
     * @return
     */
    @ApiOperation(value = "修改收货地址接口", notes = "修改收货地址接口", httpMethod = "POST")
    @PostMapping("/update")
    public Result updateAddress(@ApiParam(name = "addressBo", value = "修改收货地址前端业务Bo", required = true)
                             @RequestBody AddressBo addressBo){
        //先校验前端传来的数据是否正确
        Result result = checkAddress(addressBo);
        if(result.getStatus() != 200){
            return result;
        }
        addressService.updateAddress(addressBo);
        return Result.OK();
    }


    /**
     * 删除收货地址
     * @param userId
     * @param addressId
     * @return
     */
    @ApiOperation(value = "删除收货地址接口", notes = "删除收货地址接口", httpMethod = "POST")
    @PostMapping("/delete")
    public Result updateAddress(@ApiParam(name = "userId", value = "用户id", required = true)
                                @RequestParam String userId,
                                @ApiParam(name = "addressId", value = "收货地址id", required = true)
                                @RequestParam String addressId){
        //先校验前端传来的数据是否正确
        if(StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)){
            return Result.errorMsg("");
        }
        addressService.deleteAddress(userId, addressId);
        return Result.OK();
    }

    /**
     * 设置为默认收货地址
     * @param userId
     * @param addressId
     * @return
     */
    @ApiOperation(value = "设置为默认收货地址", notes = "设置为默认收货地址", httpMethod = "POST")
    @PostMapping("/setDefault")
    public Result setDefaultAddress(@ApiParam(name = "userId", value = "用户id", required = true)
                                        @RequestParam String userId,
                                    @ApiParam(name = "addressId", value = "收货地址id", required = true)
                                        @RequestParam String addressId){
        //先校验前端传来的数据是否正确
        if(StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)){
            return Result.errorMsg("");
        }
        addressService.setDefaultAddress(userId, addressId);
        return Result.OK();
    }

    private Result checkAddress(AddressBo addressBO) {
        String receiver = addressBO.getReceiver();
        if (StringUtils.isBlank(receiver)) {
            return Result.errorMsg("收货人不能为空");
        }
        if (receiver.length() > 12) {
            return Result.errorMsg("收货人姓名不能太长");
        }

        String mobile = addressBO.getMobile();
        if (StringUtils.isBlank(mobile)) {
            return Result.errorMsg("收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            return Result.errorMsg("收货人手机号长度不正确");
        }
        boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!isMobileOk) {
            return Result.errorMsg("收货人手机号格式不正确");
        }

        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (StringUtils.isBlank(province) ||
                StringUtils.isBlank(city) ||
                StringUtils.isBlank(district) ||
                StringUtils.isBlank(detail)) {
            return Result.errorMsg("收货地址信息不能为空");
        }

        return Result.OK();
    }
}
